package com.bdmc.report.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.report.domain.ReportPersonStop;
import com.bdmc.report.domain.bo.ReportPersonStopBo;
import com.bdmc.report.domain.vo.ReportPersonStopVo;
import com.bdmc.report.mapper.ReportPersonStopMapper;
import com.bdmc.report.service.IReportPersonStopService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工作时间内停留Service业务层处理
 *
 * @author bdmc
 * @date 2022-08-11
 */
@Service
public class ReportPersonStopServiceImpl extends ServicePlusImpl<ReportPersonStopMapper, ReportPersonStop, ReportPersonStopVo> implements IReportPersonStopService {

    @Override
    public ReportPersonStopVo queryById(Long PersonStopId){
        return getVoById(PersonStopId);
    }

    @Override
    public TableDataInfo<ReportPersonStopVo> queryPageList(ReportPersonStopBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportPersonStop> lqw = buildQueryWrapper(bo);
        Page<ReportPersonStopVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<ReportPersonStopVo> mySelectPage(ReportPersonStopBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<ReportPersonStopVo> queryList(ReportPersonStopBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<ReportPersonStop> buildQueryWrapper(ReportPersonStopBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportPersonStop> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPersonId() != null, ReportPersonStop::getPersonId, bo.getPersonId());
        lqw.eq(bo.getFenceId() != null, ReportPersonStop::getFenceId, bo.getFenceId());
        lqw.eq(bo.getStartTime() != null, ReportPersonStop::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, ReportPersonStop::getEndTime, bo.getEndTime());
        lqw.eq(bo.getStopTime() != null, ReportPersonStop::getStopTime, bo.getStopTime());
        lqw.between(params.get("beginDataTime") != null && params.get("endDataTime") != null,
            ReportPersonStop::getDataTime ,params.get("beginDataTime"), params.get("endDataTime"));
        return lqw;
    }

    @Override
    public Boolean insertByBo(ReportPersonStopBo bo) {
        ReportPersonStop add = BeanUtil.toBean(bo, ReportPersonStop.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setPersonStopId(add.getPersonStopId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ReportPersonStopBo bo) {
        ReportPersonStop update = BeanUtil.toBean(bo, ReportPersonStop.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ReportPersonStop entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
